/*
 * MyInputManager.cpp
 *
 *  Created on: 15 Oct 2013
 *      Author: Patrick
 */

#include "MyInputManager.h"

MyInputManager::MyInputManager(OIS::ParamList paramList,
		Ogre::RenderWindow* renderWnd) {

	//creates the input devices
	m_pInputMgr = OIS::InputManager::createInputSystem(paramList);

	m_pKeyboard = static_cast<OIS::Keyboard*>(m_pInputMgr->createInputObject(
			OIS::OISKeyboard, true));
	m_pMouse = static_cast<OIS::Mouse*>(m_pInputMgr->createInputObject(
			OIS::OISMouse, true));

	m_pMouse->getMouseState().height = renderWnd->getHeight();
	m_pMouse->getMouseState().width = renderWnd->getWidth();

	m_pKeyboard->setEventCallback(this);

	m_pMouse->setEventCallback(this);
}

MyInputManager::~MyInputManager() {
	if (m_pInputMgr)
		OIS::InputManager::destroyInputSystem(m_pInputMgr);
}

/*
 * keyPressed(): Implements the basic keyboard input handling
 * leave the application when escape is pressed
 * make a screenshot when print is hit
 * change polygon mode when 'M' is hit
 * toggle the frame stats and logo when 'O' is pressed
 */
bool MyInputManager::keyPressed(const OIS::KeyEvent &keyEventRef) {

	for(int i = 0; i < this->keyListeners.size(); i++) {
		this->keyListeners.at(i)->keyPressed(keyEventRef);
	}

	return true;
}

/*
 * keyReleased(): Doesn't do anything. It just has to be in there due to the class being
 * inherited from the OIS::MouseListener and OIS::KeyboardListener.
 */
bool MyInputManager::keyReleased(const OIS::KeyEvent &keyEventRef) {

	for(int i = 0; i < this->keyListeners.size(); i++) {
		this->keyListeners.at(i)->keyReleased(keyEventRef);
	}

	return true;
}

/*
 * mouseMoved(): This function gets called when the mouse is move and then changes the camera's orientation.
 */
bool MyInputManager::mouseMoved(const OIS::MouseEvent &evt) {

	for(int i = 0; i < this->mouseListeners.size(); i++) {
		this->mouseListeners.at(i)->mouseMoved(evt);
	}

	return true;
}

/*
 * mousePressed(): Doesn't do anything. It just has to be in there due to the class being
 * inherited from the OIS::MouseListener and OIS::KeyboardListener
 */
bool MyInputManager::mousePressed(const OIS::MouseEvent &evt,
		OIS::MouseButtonID id) {

	for(int i = 0; i < this->mouseListeners.size(); i++) {
		this->mouseListeners.at(i)->mousePressed(evt, id);
	}

	return true;
}

/*
 * mouseReleased(): Same here as above.
 */
bool MyInputManager::mouseReleased(const OIS::MouseEvent &evt,
		OIS::MouseButtonID id) {

	for(int i = 0; i < this->mouseListeners.size(); i++) {
		this->mouseListeners.at(i)->mousePressed(evt, id);
	}

	return true;
}

OIS::Mouse* MyInputManager::GetMouse() {
	return this->m_pMouse;
}

bool MyInputManager::isKeyDown(OIS::KeyCode key) const {
	return this->m_pKeyboard->isKeyDown(key);
}

void MyInputManager::captureInput() {
	m_pKeyboard->capture();
	m_pMouse->capture();
}

OIS::Keyboard* MyInputManager::GetKeyboard() {
	return this->m_pKeyboard;
}

void MyInputManager::registerKeyListener(OIS::KeyListener* listener) {
	this->keyListeners.push_back(listener);
}

void MyInputManager::registerMouseListener(OIS::MouseListener* listener) {
	this->mouseListeners.push_back(listener);
}
